package com.raincat.multimediapicker;

import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentStatePagerAdapter;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v4.view.ViewPager.OnPageChangeListener;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.view.animation.AnimationUtils;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.raincat.multimediapicker.ViewPagerFragment.OnImageClickListener;
import com.raincat.multimediapicker.utils.AlbumHelper;
import com.raincat.multimediapicker.utils.AlbumHelper.onImagesBucketListGetListener;
import com.raincat.multimediapicker.utils.ImageBucket;
import com.raincat.multimediapicker.utils.ImageController;
import com.raincat.multimediapicker.utils.ImageItem;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

public class ImageVisitFragment extends Fragment {
    private Context context;

    private ViewPager page;
    private RelativeLayout rl_left, rl_top, rl_check;
    private LinearLayout ll_bottom;
    private ImageView iv_check, iv_original;
    private Button btn_sure;
    private TextView tv_title, tv_original;

    private List<ImageItem> dataList = new ArrayList<>();
    private int folderIndex = -99;
    private int checkIndex = -99;

    @SuppressWarnings("deprecation")
    @Override
    public void onAttach(Activity activity) {
        super.onAttach(activity);
        context = this.getActivity();
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.picker_fragment_visit, container, false);
        rl_left = (RelativeLayout) view.findViewById(R.id.rl_left);
        rl_check = (RelativeLayout) view.findViewById(R.id.rl_check);
        ll_bottom = (LinearLayout) view.findViewById(R.id.ll_bottom);
        rl_top = (RelativeLayout) view.findViewById(R.id.rl_top);
        tv_title = (TextView) view.findViewById(R.id.tv_title);
        btn_sure = (Button) view.findViewById(R.id.btn_sure);
        iv_check = (ImageView) view.findViewById(R.id.iv_check);
        iv_original = (ImageView) view.findViewById(R.id.iv_original);
        tv_original = (TextView) view.findViewById(R.id.tv_original);
        page = (ViewPager) view.findViewById(R.id.viewpager);

        if (!ImageController.showOriginalOption) {
            iv_original.setVisibility(View.INVISIBLE);
            tv_original.setVisibility(View.INVISIBLE);
            tv_original.setClickable(false);
        }
        listener();
        return view;
    }

    private void setData() {
        PagerAdapter pagerAdapter = new ScreenSlidePagerAdapter(getChildFragmentManager());
        page.setAdapter(pagerAdapter);
        page.setCurrentItem(checkIndex);

        tv_title.setText((checkIndex + 1) + "/" + dataList.size());
        setCheck(checkIndex);
        setCount();
    }

    private void listener() {
        rl_left.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                ((PickerActivity) context).fragmentTabAdapter.removeFragment();
            }
        });

        rl_check.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                View view = ((RelativeLayout) v).getChildAt(0);
                if (ImageController.addOrRemoveImageItem(context, dataList.get(checkIndex).imagePath)) {
                    ((ImageView) view).setImageResource(R.mipmap.picker_check);
                    view.setBackgroundResource(R.drawable.picker_check_enable);
                } else {
                    ((ImageView) view).setImageDrawable(null);
                    view.setBackgroundResource(R.drawable.picker_check_disable);
                }
                setCount();
            }
        });
        btn_sure.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View arg0) {
                if (ImageController.count != 0) {
                    if (ImageController.Mode == ImageController.TAKE) {
                        finishActivity();
                    } else if (ImageController.Mode == ImageController.CROP) {
                        ((PickerActivity) context).fragmentTabAdapter.addFragment(new ImageCropFragment(), null);
                    }
                }
            }
        });
        tv_original.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View arg0) {
                ImageController.isOriginal = !ImageController.isOriginal;
                setOriginal();
            }
        });
        page.addOnPageChangeListener(new OnPageChangeListener() {
            @Override
            public void onPageSelected(int arg0) {
            }

            @Override
            public void onPageScrolled(int arg0, float arg1, int arg2) {
            }

            @Override
            public void onPageScrollStateChanged(int arg0) {
                checkIndex = page.getCurrentItem();
                tv_title.setText((checkIndex + 1) + "/" + dataList.size());
                setCheck(checkIndex);
            }
        });
    }

    private void setCheck(int index) {
        if (ImageController.imageItemMap.get(dataList.get(index).imagePath) != null) {
            iv_check.setBackgroundResource(R.drawable.picker_check_enable);
            iv_check.setImageResource(R.mipmap.picker_check);
        } else {
            iv_check.setBackgroundResource(R.drawable.picker_check_disable);
            iv_check.setImageDrawable(null);
        }
    }

    private void setCount() {
        if (btn_sure != null) {
            if (ImageController.count == 0) {
                btn_sure.setBackgroundResource(R.drawable.picker_btn_disable);
                btn_sure.setText(context.getResources().getString(R.string.picker_OK));
            } else {
                btn_sure.setBackgroundResource(R.drawable.picker_btn_enable);
                btn_sure.setText(context.getResources().getString(R.string.picker_OK) + "(" + ImageController.count + "/" + ImageController.maxCount + ")");
            }
            if (ImageController.isOriginal)
                tv_original.setText(context.getResources().getString(R.string.picker_original) + ImageController.getImageSize());
        }
    }

    private void setOriginal() {
        if (iv_original != null) {
            if (!ImageController.isOriginal) {
                iv_original.setImageDrawable(null);
                tv_original.setText(context.getResources().getString(R.string.picker_original));
            } else {
                iv_original.setImageResource(R.drawable.picker_iv_enable);
                tv_original.setText(context.getResources().getString(R.string.picker_original) + ImageController.getImageSize());
            }
        }
    }

    public void finishActivity() {
        if (ImageController.isOriginal) {
            ImageController.takeRealPathList();
            ((PickerActivity) context).finishActivity("ImageList", ImageController.realPathList);

        } else {
            ImageController.takeZoomPathList(context, new ImageController.OnAllDecodeListener() {
                @Override
                public void onResult() {
                    ((PickerActivity) context).finishActivity("ImageList", ImageController.zoomPathList);

                }
            });
        }
    }

    private class ScreenSlidePagerAdapter extends FragmentStatePagerAdapter {
        ScreenSlidePagerAdapter(FragmentManager fm) {
            super(fm);
        }

        @Override
        public Fragment getItem(int position) {
            ViewPagerFragment fragment = new ViewPagerFragment(new OnImageClickListener() {
                @Override
                public void onClick() {
                    if (ll_bottom.getVisibility() == View.GONE) {
                        ll_bottom.setVisibility(View.VISIBLE);
                        rl_top.setVisibility(View.VISIBLE);
                        ll_bottom.startAnimation(AnimationUtils.loadAnimation(context, R.anim.fade_ins));
                        rl_top.startAnimation(AnimationUtils.loadAnimation(context, R.anim.fade_ins));
                    } else {
                        ll_bottom.startAnimation(AnimationUtils.loadAnimation(context, R.anim.fade_outs));
                        rl_top.startAnimation(AnimationUtils.loadAnimation(context, R.anim.fade_outs));
                        ll_bottom.setVisibility(View.GONE);
                        rl_top.setVisibility(View.GONE);
                    }
                }
            });
            fragment.setImageItem(dataList.get(position));
            return fragment;
        }

        @Override
        public int getCount() {
            return dataList.size();
        }
    }

    @Override
    public void onActivityCreated(Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
    }

    @Override
    public void onStart() {
        super.onStart();
        if (isAdded() && checkIndex == -99) {
            checkIndex = getArguments().getInt("checkIndex", -1);
            folderIndex = getArguments().getInt("folderIndex", -1);

            if (folderIndex != -1) {
                AlbumHelper.getInstance(context).getImagesBucketList(new onImagesBucketListGetListener() {
                    @Override
                    public void onFinish(List<ImageBucket> bucketList) {
                        dataList = bucketList.get(folderIndex).imageList;
                        ((Activity) context).runOnUiThread(new Runnable() {
                            public void run() {
                                setData();
                            }
                        });
                    }
                });
            } else {
                for (ImageItem imageItem : ImageController.imageItemMap.values()) {
                    Collections.addAll(dataList, imageItem);
                }
                setData();
            }
        }
    }

    @Override
    public void onResume() {
        super.onResume();
        setOriginal();
    }

    @Override
    public void onPause() {
        super.onPause();
    }

    @Override
    public void onStop() {
        super.onStop();
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
    }

    @Override
    public void onDetach() {
        super.onDetach();
    }
}
